<template>
  <div id="adjustment" v-loading="loading">
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" :title="$t('ex.adjust')" @del="del"
      @newCreate="newCreate" @upFile="upFile" @downloadAction="downloadAction" @uploadDataAction="uploadDataAction"
      @confirmAction="confirmAction"></v-tableTitle>
    <v-table :page="page" :pageSize="pageSize" :tableConfig="tableConfig" :tableData="tableData" :total="total"
      @findData="getTableData" @getTableData="getTableData" @searhData="searhData" @selectData="selectData"
      @setdialog="setdialog" @upData="getTableData" ref="table"></v-table>

  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";
import bus from "../../../../bus/bus"

export default {
  name: "adjustment",
  mixins: [baseMixin, config],
  data() {
    return {
      loading: false,
      tableConfig: [
        {
          label: this.$t('in.adjustmentkey'),
          prop: "adjustmentkey",
          input: "input",
        },
        {
          label: this.$t('config.shipper'),
          prop: "storerkey",
          input: "input",
        },
        /*
        {
            label: this.$t('in.asnType'),
            prop: "rtxudf1",
            input: "input",
        },
        */
        {
          label: this.$t('in.asnType'),
          prop: "rtxudf1",
          input: "select",
          options: [
            { code: ' ', description: this.$t('code.status7') },
            { code: 'DeliveryIn', description: this.$t('code.status15') },
            { code: 'ReturnIn', description: this.$t('code.status16') },
          ]
        },
        {
          label: this.$t('ex.rtxudf2'),
          prop: "rtxudf2",
          input: "input",
        },
        {
          label: this.$t('ex.rtxudf3'),
          prop: "rtxudf3",
          input: "input",
        },
        {
          label: this.$t('in.externreceiptkey2'),
          prop: "adjustmentkey2",
          input: "input",
        },
        {
          label: this.$t('app.addDate'),
          prop: "adddate",
          input: "date",
        },
        {
          label: this.$t('common.status'),
          prop: "status",
          input: "select",
          options: [
            { code: '0', description: this.$t('code.status7') },
            { code: '5', description: this.$t('code.status8') },
            { code: '10', description: this.$t('code.status9') },
            { code: '21', description: this.$t('code.status10') },
            { code: '22', description: this.$t('code.status11') },
            { code: '25', description: this.$t('code.status12') },
            { code: '30', description: this.$t('code.status14') },
          ]
        },
        {
          label: this.$t('app.addName'),
          prop: "addwho",
          input: "input",
        },
        {
          label: this.$t('ex.effectivedate'),
          prop: "effectivedate",
          input: "date",
        }
      ],
      tableData: [],
      pageSize: 10,
      page: 1,
      total: 10,
      tableSelectData: [],
      tableSearhData: {},
    };
  },
  computed: {
    btnConfig() {
      return [
        { type: "file1", label: this.$t('common.import'), certigier: !this.btnPermNameList.includes('WM06020401'), invalid: "file" },
        { label: this.$t('common.export'), disabled: true, certigier: !this.btnPermNameList.includes('WM06020402'), invalid: "downloadAction" },
        { label: this.$t('common.create'), certigier: !this.btnPermNameList.includes('WM06020403'), invalid: "newCreate" },
        { label: this.$t('common.delete'), certigier: !this.btnPermNameList.includes('WM06020404'), disabled: true, invalid: "del" },
        { label: this.$t('ex.uploadDataAction'), certigier:!this.btnPermNameList.includes('WM06020405'), disabled: true, invalid: "uploadDataAction" },
        { label: this.$t('ex.confirmAction'), certigier:!this.btnPermNameList.includes('WM06020406'), disabled: true, invalid: "confirmAction" },
      ]
    }
  },
  created() {
  },
  methods: {
    async upFile(data) {
      try {
        let res = await this.$api.post('/importAdjustment', data);
        
          this.$message.success(res.returnMgr);
          let page = sessionStorage.getItem('page');
          this.getTableData(page, this.tableSearhData);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async downloadAction(data) {
      try {
        let res = await this.$api.post('/exportAdjustment', data, { responseType: 'blob' })
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, 'Adjustment.xls');
        } else {
          var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8' });
          var filename = 'Adjustment.xls';
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    async uploadDataAction(data) {
      try {
        let res = await this.$api.post(`/uploadDataAdjustment`, data);
        
          this.$message.success(res.returnMgr);
          let page = sessionStorage.getItem('page');
          this.getTableData(page, this.tableSearhData);
          
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async confirmAction(data) {
      try {
        let res = await this.$api.post(`/confirmAdjustment`, data);
        
          this.$message.success(res.returnMgr);
          let page = sessionStorage.getItem('page');
          this.getTableData(page, this.tableSearhData);
          
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    //新建
    async newCreate() {
      this.$store.commit("setNewFlag", true);
      this.$router.push({ name: 'adjustmentDeawer' });
      // this.editModule.title = this.$t('ex.adjustAdd');
      // this.editModule.flag = true;
      // this.editModule.activeId = 0;
      // this.editModule.data = null;
    },
    //选中的内容
    selectData(data) {
      this.tableSelectData = data;
    },
    //删除
    async del(data) {
      try {
        let res = await this.$api.post(`/deleteAdjustment`, data);
        
          this.$message.success(res.returnMgr);
          let page = sessionStorage.getItem('page');
          this.getTableData(page, this.tableSearhData);
          
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async getTableData(page, data) {
      try {
        this.loading = true;
        let res = await this.$api.post(`/queryAdjustmentList/${page}`, data);
        
          this.tableData = res.page.list;
          this.page = res.page.curpage;
          this.pageSize = res.page.pageSize;
          this.total = res.page.total;
        
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.loading = false;
      }
    },
    //搜索的值
    searhData(data, up) {
      if (!up) {
        this.tableSearhData = data;
      } else {
        this.tableSearhData = data;
        this.getTableData(1, this.tableSearhData);
      }
    },
    //设置弹框
    setdialog(data) {
      this.$store.commit("setRouteData", data);
      this.$store.commit("setNewFlag", false);
      this.$router.push({ name: 'adjustmentDeawer',query:{adjustmentkey:data.adjustmentkey} });
      // this.editModule.title = this.$t('ex.adjustEdit');
      // this.editModule.flag = true;
      // this.editModule.activeId = data.id;
      // this.editModule.data = data;
    },
  },
  mounted() {
    this.$store.commit("setSkip", {});
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", {});
    this.getTableData(1, {});
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-table": table,
  }
};
</script>

<style scoped>
#adjustment {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
